package patrol.mobile.com.mobilepatrol.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.List;

import patrol.mobile.com.mobilepatrol.R;

/**
 * Created by shjshi on 2018/1/15.
 */

public class EditListAdapter extends BaseAdapter {

    private List<SongBean> mData;


    public void setData(List<SongBean> data) {
        mData = data;
        this.notifyDataSetChanged();
    }

    public void addItem() {
        mData.add(0, new SongBean());
        this.notifyDataSetChanged();
    }

    public void deletItem(int pos) {
        mData.remove(pos);
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_edit_test, null);
        TextView posTxt = (TextView) view.findViewById(R.id.postion_txt);
        EditText contentTxt = (EditText) view.findViewById(R.id.content_edit);
        Button deletBtn = (Button) view.findViewById(R.id.delet_btn);
        posTxt.setText("第" + (position + 1) + "条");
        contentTxt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.e("onTextChanged-----", s.toString());
                if (s.toString().equals("")){
                    mData.get(position).setNum(0);
                }else{
                    mData.get(position).setNum(Double.valueOf(s.toString()));
                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        if (mData.get(position).getNum()==0){
            if (contentTxt==null)
            contentTxt.setText("");
        }else{
            contentTxt.setText(mData.get(position).getNum()+"");
        }

        deletBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deletItem(position);
            }
        });

        return view;
    }

    private class ViewHolder {
        public TextView posTxt;
        public EditText contentTxt;
        public Button deletBtn;

    }

    public static class SongBean {
        private String name;
        private String author;
        private double num;

        public double getNum() {
            return num;
        }

        public void setNum(double num) {
            this.num = num;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getAuthor() {
            return author;
        }

        public void setAuthor(String author) {
            this.author = author;
        }
    }
}
